
#include "RequestRegister.h"
#include "Server.h"

namespace Engine {

void CRequestRegister::DoRequest() {
	
	if(GetParameter(string("add-device")).length() == 0) {
		Response(RESPONSE_STATUS_BadRequest);
		return;
	}

	string authcode;
	CServer::GetInstance()->createAuthCode(mDevice->GetUser(), authcode);

	JsonBuffer ss;
	JsonWriter writer(ss);
	writer.StartObject();
	writer.String("authcode").String(authcode.c_str());
	writer.String("valid-seconds").Int(AUTHCODE_ALIVE_SECONDS);
	writer.EndObject();

	ResponseRef response = InitResponse();
	response->SetContent(string(ss.GetString()), string(PACKAGE_CONTENT_TYPE_JSON));
	response->SetStatus(RESPONSE_STATUS_OK);
	mDevice->SendPackage(response);
}

}